package site.guns.controller;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import site.guns.couchdb.GunsDb;
import site.guns.domain.GunInfo;


@Controller
@RequestMapping("/")
public class RootController {
    @Autowired
    private GunsDb gunsDb;

    @RequestMapping(method = RequestMethod.GET)
    public String get(Model model) {
        List<GunInfo> guns = gunsDb.getBuiltInView().<GunInfo>createDocsQuery().asDocs();

        Collections.sort(guns, new Comparator<GunInfo>() {
            @Override
            public int compare(GunInfo o1, GunInfo o2) {
                return Integer.compare(o1.getPrice(), o2.getPrice());
            }
        });

        model.addAttribute("realisticGuns", guns);

        return "/main";
    }
}
